#include <QtNetwork/QLocalSocket>
#include "localserver.h"
#include "mainwindow.h"

LocalServer::LocalServer(QObject* parent) :
  QObject(parent)
{
  this->win = NULL;
}

LocalServer::LocalServer(MainWindow* win) :
  QObject(NULL)
{
  this->win = win;
}

LocalServer::~LocalServer()
{
}

void LocalServer::init()
{
  srv = new QLocalServer();
  srv->listen("rift-tool-app-srv");
  connect(srv, SIGNAL(newConnection()), this, SLOT(newConnection()));
  if(win != NULL) connect(this, SIGNAL(showUp()), win, SLOT(showUp()));
}

void LocalServer::done()
{
  srv->deleteLater();
}

void LocalServer::newConnection()
{
  QLocalSocket* sck = srv->nextPendingConnection();
  if(sck->waitForReadyRead())
    {
      QByteArray data = sck->readAll();
      if(data == "show")
        emit showUp();
    }
  sck->disconnectFromServer();
  sck->close();
  delete sck;
}
